package game.view.mainscene
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import game.core.LayerManager;
	import game.core.StageProxy;
	import game.define.MediatorName;
	import game.define.NotificationName;
	import game.define.typedef.PlayerActionType;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MainSceneMediator extends Mediator
	{
		public function MainSceneMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(MediatorName.MAIN_SCENE_MEDIATOR, new MainSceneView());
			
			LayerManager.sceneLayer.addChild(view);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				NotificationName.APP_START_COMPLETE,
				NotificationName.SCENE_PLAYER_MOVED
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case NotificationName.APP_START_COMPLETE:
					initialize();
					break;
				case NotificationName.SCENE_PLAYER_MOVED:
					centerMap();
					break;
			}
		}
		
		//////////////////////////////////////////////////////////////////
		//
		
		private function initialize():void
		{
			// map
			var mapIns:Sprite = new Sprite();
			view.mapLayer.addChild(mapIns);
			for (var i:int=0; i<100; i++)
			{
				for (var j:int=0; j<100; j++)
				{
					var grid:Shape = new Shape();
					grid.graphics.beginFill(0xFFFFFF * Math.random());
					grid.graphics.drawRect(0, 0, 25, 25);
					grid.graphics.endFill();
					grid.x = j * grid.width;
					grid.y = i * grid.height;
					grid.alpha = 0.2;
					mapIns.addChild(grid);
				}
			}
			
			// anim
			view.playerSelf.setImage(1);
			view.playerSelf.playAction(PlayerActionType.STAND_LOWER);
			view.playerSelf.move(300, 200, 0);
			view.playerSelf.setName("tom");
			
			//
			view.addEventListener(MouseEvent.CLICK, onSceneLayerClick);
		}
		
		protected function onSceneLayerClick(event:MouseEvent):void
		{
			view.playerSelf.move(event.stageX, event.stageY);
		}
		
		private function get view():MainSceneView
		{
			return viewComponent as MainSceneView;
		}
		
		private function centerMap():void
		{
			view.mapLayer.x = -(view.playerSelf.x - StageProxy.rootWidth/2);
			view.mapLayer.y = -(view.playerSelf.y - StageProxy.rootHeight/2);
		}	
	}
}